'use strict';
const Controller = require('egg').Controller;

class UserController extends Controller {

  /**
     *@apiDeprecated The API not enabled
     *@api {post} /login 用户登录
     *@apiName login
     *@apiDescription 系统用户登录
     *@apiGroup User-not enabled
     *@apiParam {string} username  user name
     *@apiParam {string} password  user password
     *@apiSuccess {int} errcode  错误码
     *@apiSuccess {string} message 提示语
     *@apiSuccess {Object} data 数据对象
     *@apiSuccess {string} data.user_id 用户ID
     *@apiSuccess {string} data.username 用户名
     *@apiSuccess {int} data.status 用户状态 0:作废 1:正常
     *@apiSuccess {string} data.token 验证token
     */
  async login() {
    const { ctx, service } = this;
    const { username, password } = ctx.request.body;
    if (!username) {
      return ctx.json({ errcode: ctx.ERRCODE.DATA_ERROR, message: '用户名不能为空' });
    }
    if (!password) {
      return ctx.json({ errcode: ctx.ERRCODE.DATA_ERROR, message: '密码不能为空' });
    }
    const reponse = await service.user.login(username, password);
    ctx.json(reponse);
  }

  /**
     *@apiDeprecated The API not enabled
     *@api {put} /user  修改用户
     *@apiName modify-user
     *@apiDescription  修改用户密码
     *@apiGroup User-not enabled
     *@apiPermission 用户认证
     *@apiHeader {String} token 用户认证字段
     *@apiParam {string} username  old user username
     *@apiParam {string} old_password  old user password
     *@apiParam {string} new_password  new user password
     *@apiSuccess {int} errcode  错误码
     *@apiSuccess {string} message 提示语
     */
  async modifyUser() {
    const { ctx, service } = this;
    const body = ctx.request.body;
    const changeres = await service.user.modifyUser(ctx.author, body);
    ctx.json(changeres);
  }

  /**
     *@apiDeprecated The API not enabled
     *@api {post} /refresh-token  刷新token认证
     *@apiName refreshToken
     *@apiDescription   刷新token认证
     *@apiGroup User-not enabled
     *@apiPermission 用户认证
     *@apiHeader {String} token 用户认证字段
     *@apiSuccess {int} errcode  错误码
     *@apiSuccess {string} message 提示语
     *@apiSuccess {string} token 新token
     */
  async refreshToken() {
    const { ctx, service } = this;
    try {
      const token = ctx.request.header.token;
      if (token) {
        const reponse = await service.user.refreshToken(token);
        return ctx.json(reponse);
      }
      throw new Error('认证字段缺失');
    } catch (error) {
      let code = 403;
      const response = { error: error.message };
      switch (error.name) {
        case 'TokenExpiredError':
          code = 401;
          response.message = '刷新认证过期,请重新登录';
          response.errcode = ctx.ERRCODE.AUTH_EXPIRE;
          break;
        case 'JsonWebTokenError':
          response.errcode = ctx.ERRCODE.AUTH_FAIL;
          response.message = '刷新认证失败,请重新登录';
          break;
        default:
          response.errcode = ctx.ERRCODE.AUTH_ERROR;
          response.message = '刷新认证异常,请重新登录';
          break;
      }
      ctx.json(response, code);
    }
  }

  /**
     *@apiDeprecated The API not enabled
     *@api {get} /user-list  获取用户列表
     *@apiName user-list
     *@apiDescription  获取用户列表
     *@apiGroup User-not enabled
     *@apiPermission 用户认证
     *@apiHeader {String} token 用户认证字段
     *@apiParam {int} [page=1] 页码
     *@apiParam {int} [page_size=20] 每页大小
     *@apiParam {int} [status]  用户状态 0:作废 1:正常 无：全部
     *@apiParam {string} [username]  用户名 支持模糊搜索
     *@apiSuccess {int} errcode  错误码
     *@apiSuccess {string} message 提示语
     *@apiSuccess {int} count 查询总数
     *@apiSuccess {Array[Object]} rows 数据列表
     *@apiSuccess {string} rows.user_id 用户ID
     *@apiSuccess {string} rows.username 用户名
     */
  async userList() {
    const { ctx, service } = this;
    const query = ctx.query || {};
    query.page = Number(query.page) || 1;
    query.page_size = Number(query.page_size) || 20;
    const reponse = await service.user.userList(query);
    ctx.json(reponse);
  }

}

module.exports = UserController;
